<?php

/**
 * BaseAjax,处理后台操作的类
 * @Author : bapijun
 */
namespace inc\BaseAjax;
//require('../global.php');
class BaseAjax
{
    private $foldName;//上传文件夹的名称
    
    /**
     * 通过解析对应的request参数,选择对应的函数进行处理
     * @Author : bapijun
     */
    public function dealWithAjax()
    {
        if (isset($_REQUEST['ac']) && !empty($_REQUEST['ac'])) {
            $func =  $_REQUEST['ac'];
            $this->$func();//调用可变函数
        } else {
            throw new \Exception("Can't found parame ac");
        }
    }
    /**
     * 图片上传处理函数
     * @Author : bapijun
     */
    protected function upload()
    {
        include('inc/Uploader.class.php');
        $config = array(
            'pathFormat' => 'uploads/' . $this->foldName . '/{time}{rand:6}', //保存路径
            'allowFiles' => array('.png','.jpg','.jpeg','.gif'), //文件允许格式
            'maxSize' => 2097152 //文件大小限制，单位B
        );
        $up = new \Uploader('file', $config);
        $info = $up->getFileInfo();
        if($info['state'] == 'SUCCESS'){
            $url = explode('/', $info['url']);
            $url[count($url) - 1] = 's_'.$url[count($url) - 1];
            $url = implode('/', $url);
            //imgsresize($info['url'], 200, 200, $url);
        }
        echo '{"url":"'.$info['url'].'","fileType":"'.$info['type'].'","original":"'.$info['original'].'","preview":"'.SITEROOTURL.$info['url'].'","state":"'.$info['state'].'"}';
    }
    /**
     * 构造函数
     * @parame $foldName 定义的上传文件夹位置
     * @Author : bapijun
     */
    public function __construct($foldName='img')
    {
        $this->foldName = $foldName;
    }

    function __destruct()
    {}
}

